#include "PlayState.h"
#include "..\Managers\StateManager.h"
#include "..\Components\GameObject.h"
#include "..\Components\IComponent.h"
#include "..\Managers\InputManager.h"
#include "..\ScaleFactor.h"
#include "..\EnumDefine.h"
#include "..\Components\PhysicsComponent.h"
#include "..\Define.h"
#include "..\Managers\RenderManager.h"
#include "..\Engines\RenderEngine.h"
#include "..\Managers\GameObjectManager.h"
#include "..\Components\PlayerHealthComponent.h"
#include "..\Factory\RockmanFactory.h"
#include "..\Level\Level.h"
#include "..\Factory\BladerFactory.h"
#include "..\Factory\MetallFactory.h"
#include "..\Factory\BladerSpawner.h"
#include "..\States\PauseState.h"
#include "..\rapidxml.hpp"
#include "..\Generic\KillCounter.h"
#include <fstream>


PlayState::PlayState(bool p_value) : m_loadGame(p_value){}
PlayState::~PlayState(){}

void PlayState::initialize()
{
	m_camera = RenderManager::getSingleton().getRenderEngine()->createCamera(1, "main");
	m_camera->setVelocity(2.0f);
	m_camera->setPosition(VSW_05, VSH_05);

	RockmanFactory rockmanFactory;
	BladerFactory bladerFactory;
	MetallFactory metallFactory;
	m_gameObjectManager = new GameObjectManager;

	GameObject* player = rockmanFactory.createPlayer(vn::Vector2(200.0f, 100.0f));
	player->setGameObjectManager(m_gameObjectManager);
	m_gameObjectManager->addGameObject(player);
	m_kill_counter = new KillCounter(100);
	if (m_loadGame)
	{
		loadGame(player);	
	}

	m_level = new Level("levels\\level01.xml");
	m_level->loadLevel();
	m_level->playMusic(true);
	GameObject* blader = bladerFactory.createEnemy(vn::Vector2(50.0f, 50.0f), "player");
	blader->setGameObjectManager(m_gameObjectManager);
	m_gameObjectManager->addGameObject(blader);

	m_bladerSpawner = new BladerSpawner(m_gameObjectManager, 1000, 2000);
	GameObject* met = metallFactory.createEnemy(vn::Vector2(50.0f,	50.0f), "player");
	met->setGameObjectManager(m_gameObjectManager);
	m_gameObjectManager->addGameObject(met);
	

}

void PlayState::shutdown()
{
	delete m_kill_counter;
	m_kill_counter = nullptr;
	delete m_bladerSpawner;
	m_bladerSpawner = nullptr;
	delete m_gameObjectManager;
	m_gameObjectManager = nullptr;
	m_level->playMusic(false);
	delete m_level;
	m_level = nullptr;
	RenderManager::getSingleton().getRenderEngine()->clearAll();
}

void PlayState::handleEvents(){}

void PlayState::update(const GameTime& p_gameTime)
{	
	m_gameObjectManager->update(p_gameTime);
	m_bladerSpawner->update(p_gameTime);
	if (InputManager::getSingleton().isKeyPressed("pause"))
		StateManager::getSingleton().pushState(new PauseState(m_gameObjectManager, m_kill_counter));
}

void PlayState::loadGame(GameObject* p_gameObject)
{
	std::ifstream file("saves\\save.xml");
	if (file)
	{
		std::string xmlContent;
		std::string line;
		while (std::getline(file, line))
			xmlContent += line;
		file.close();
		std::vector<char> xmlBuffer = std::vector<char>(xmlContent.begin(), xmlContent.end());
		xmlBuffer.push_back('\0');
		rapidxml::xml_document<> doc;
		doc.parse<rapidxml::parse_no_data_nodes>(&xmlBuffer[0]);
		rapidxml::xml_node<>* root = doc.first_node();
		float x = static_cast<float>(atof(root->first_attribute("x")->value()));
		float y = static_cast<float>(atof(root->first_attribute("y")->value()));
		int health = atoi(root->first_attribute("health")->value());
		int kill_count = atoi(root->first_attribute("killcount")->value());
		static_cast<PlayerHealthComponent*>(p_gameObject->getComponent("HealthComponent"))->setHealth(health);
		static_cast<PhysicsComponent*>(p_gameObject->getComponent("PhysicsComponent"))->setPosition(vn::Vector2(x, y));
		m_kill_counter->setCurrentKillCount(kill_count);
	}
}